﻿package net.wangit.dev.version;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.context.ActionResult;
import net.wangit.dev.DomainAction;
import net.wangit.etl.LoadResult;
import net.wangit.etl.Loader;
import net.wangit.jdbc.DB;
import net.wangit.util.Checker;

public class InputVersionAction extends DomainAction {

	public InputVersionAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		ActionResult result = ac.getActionResult();
		String path = ac.getRequestParameter("input.path");
		try {
			LoadResult loadResult = Loader.load(db, path);
			String fileNames[] = loadResult.getFileNames();
			if (Checker.isEmpty(fileNames)) {
				result.addInfo(LabelUtil.get(ac, "dev", "input.unselected"));
			} else {
				result.addInfo(LabelUtil.get(ac, "dev", "input.1"));
				String tableNames[] = loadResult.getTableNames();
				int counts[] = loadResult.getCounts();
				for (int i = 0; i < fileNames.length; i++) {
					String params[] = { tableNames[i],
							Integer.toString(counts[i]), fileNames[i] };
					result.addInfo(LabelUtil.get(ac, "dev", "input.result",
							params));
				}

			}
		} catch (Exception e) {
			result.setSuccessful(false);
			result.addError(LabelUtil.get(ac, "dev", "input.0"));
			throw e;
		}
	}
}
